<template>
  <a-modal
    title="发送朋友圈"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="动态内容"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入动态内容" v-decorator="['content', {rules: [{required: true, message: '请输入动态内容！'}]}]" />
        </a-form-item>
<!--        <div style="margin-left: 100px">-->
        <a-form-item
          label="请上传"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-upload
            :customRequest="customRequest"
            :multiple="true"
            :showUploadList="false"
            name="file"
          >
            <a-button>
              <a-icon type="upload"/>
              <span >上传图片</span>
            </a-button>
          </a-upload>
          <!--图片显示-->
          <div style="display: flex; flex-direction: row; flex-wrap: wrap; margin-top: 10px">
            <div v-for="(image,index) in mediaImgSrc" :key="index"  @click="getIndexMultiple(text,index)">
              <img :src="image" style="width: 100px;height: 100px; margin-right:2px" >
            </div>
          </div>
        </a-form-item>
<!--        </div>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import { appDynamicAdd } from '@/api/modular/app/dynamic/appDynamicManage'
  import { sysFileInfoUploadFront } from '@/api/modular/system/fileManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        userId: '',
        //
        fileBucket: 'trends', // 第一层级桶名
        mediaImgSrc: [], //
        mediaImgSrcIds: []
      }
    },
    methods: {
      // 初始化方法
      add (userId) {
        console.log(userId)
        this.userId = userId
        this.visible = true
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            // console.log(this.mediaImgSrc)
            // console.log(this.mediaImgSrcIds)
            values.fileIds = this.mediaImgSrcIds.join(',')
            values.userId = this.userId
            appDynamicAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.mediaImgSrc = []
        this.mediaImgSrcIds = []
      },
      /**
       * 上传文件
       */
      customRequest (data) {
        const formData = new FormData()
        formData.append('file', data.file)
        formData.append('fileBucket', this.fileBucket)
        formData.append('userId', this.userId)
        sysFileInfoUploadFront(formData).then((res) => {
          if (res.success) {
            this.mediaImgSrcIds.push(res.data.filedId)
            this.mediaImgSrc.push(res.data.sysFileInfo)
            this.$message.success('上传成功')
          } else {
            this.$message.error('上传失败：' + res.message)
          }
        })
      }
    }
  }
</script>
